//
//  LSTabBar.swift
//  GenericProjectForSwiftUI
//
//  Created by 皮蛋菌 on 2023/4/16.
//

import SwiftUI

struct LSTabBar: View {
    
    @State var selectIndex : Int = 0
    
    init() {
        Config.setup()
    }
    
    private var currentTitle : String {
        switch self.selectIndex {
        case 0:
            return "首页"
        case 1:
            return "社区"
        case 2:
            return "消息"
        case 3:
            return "我的"
        default:
            return ""
        }
    }
    
    var body: some View {
        NavigationView {
            TabView(selection: $selectIndex) {
                OneViewController().tabItem {
                    TabItemView(style: TabItemView.Style.home, selectedIndex: selectIndex)
                }.tag(0)
                TwoViewController().tabItem {
                    TabItemView(style: TabItemView.Style.community, selectedIndex: selectIndex)
                }.tag(1)
                ThreeViewController().tabItem {
                    TabItemView(style: TabItemView.Style.message, selectedIndex: selectIndex)
                }.tag(2)
                FourViewController().tabItem {
                    TabItemView(style: TabItemView.Style.mine, selectedIndex: selectIndex)
                }.tag(3)
            }
            .navigationBarTitle(currentTitle, displayMode: .inline)
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

struct TabItemView: View {
    enum Style: Int {
        case home
        case community
        case message
        case mine
    }
    
    let style: Style
    let selectedIndex: Int
    
    var body: some View {
        ZStack {
            VStack {
                Image(style.imageName + (selectedIndex == style.rawValue ? "_selected" : ""))
                Text(style.tabName)
                    .font(.system(size: 12))
                    .foregroundColor((selectedIndex == style.rawValue ? Color.green : Color.black))
            }
        }
    }
}

extension TabItemView.Style {
    var imageName: String {
        switch self {
        case .home:
            return "tabbar_home"
        case .community:
            return "tabbar_reward"
        case .message:
            return "tabbar_message_center"
        case .mine:
            return "tabbar_profile"
        }
    }
}

extension TabItemView.Style {
    var tabName: String {
        switch self {
        case .home:
            return "首页"
        case .community:
            return "社区"
        case .message:
            return "消息"
        case .mine:
            return "我的"
        }
    }
}

struct LSTabBar_Previews: PreviewProvider {
    static var previews: some View {
        LSTabBar()
    }
}
